package com.tiancheng.trade.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.order.enums.OrderPayStatusEnum;
import com.tiancheng.trade.order.enums.OrderStatusEnum;
import com.tiancheng.trade.order.mapper.OrderBatchMapper;
import com.tiancheng.trade.order.model.Order;
import com.tiancheng.trade.order.model.OrderBatch;
import com.tiancheng.trade.order.service.OrderBatchService;
import com.tiancheng.trade.order.vo.api.OrderBatchVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2023/5/30 15:02
 */
@Service
public class OrderBatchServiceImpl implements OrderBatchService {
    @Resource
    private OrderBatchMapper orderBatchMapper;
    @Override
    public void saveOrderBatch(OrderBatchVO orderBatchVO, List<Order> orders) {
        // 校验订单
        if(orderBatchVO.getOrders().size()!=orders.size()){
            BusinessErrorInfoEnum.ORDER_CREATE_BATCH_CHECK_ERROR.assertFail();
        }
        for (OrderBatchVO.Order order : orderBatchVO.getOrders()) {
            final Optional<Order> any = orders.stream().filter(o -> o.getOrderId().equals(order.getOrderId())).findAny();
            BusinessErrorInfoEnum.ORDER_CREATE_BATCH_CHECK_ERROR.assertIsTrue(any.isPresent());
        }
        final OrderBatch orderBatch = new OrderBatch();
        orderBatch.setCreatedDt(orderBatchVO.getCreateTime());
        orderBatch.setBatchId(orderBatchVO.getBatchId());
        orderBatch.setUid(orderBatchVO.getUid());
        orderBatch.setOrderSource(orderBatchVO.getOrderSource());
        orderBatch.setOrderName(orderBatchVO.getOrderName());
        orderBatch.setOrderStatus(OrderStatusEnum.waiting_pay.getCode());

        orderBatch.setOrderAmount(orders.stream().mapToInt(Order::getOrderAmount).sum());
        orderBatch.setPayAmount(orders.stream().mapToInt(Order::getPayAmount).sum());
        orderBatch.setOrderCnt(orders.size());
        orderBatch.setPayStatus(OrderPayStatusEnum.un_pay.getCode());
        orderBatch.setTradeInfo(orderBatchVO.getTradeInfo());
        orderBatch.setClientId(orderBatchVO.getSystemSource());
        orderBatchMapper.insert(orderBatch);
    }

    @Override
    public boolean close(String uid, String batchId) {
        final OrderBatch orderBatch = orderBatchMapper.selectOne(new LambdaQueryWrapper<OrderBatch>().eq(OrderBatch::getUid, uid).eq(OrderBatch::getBatchId, batchId));
        BusinessErrorInfoEnum.ORDER_CLOSE_NO_DATA.assertIsTrue(null!=orderBatch);
        BusinessErrorInfoEnum.ORDER_CLOSE_STATUS_ERROR.assertIsTrue(!orderBatch.getOrderStatus().equalsIgnoreCase(OrderStatusEnum.paid.getCode()));
        if(orderBatch.getOrderStatus().equalsIgnoreCase(OrderStatusEnum.cancelled.getCode())){
            return true;
        }
        final OrderBatch updateEntity = new OrderBatch();
        updateEntity.setId(orderBatch.getId());
        updateEntity.setOrderStatus(OrderStatusEnum.cancelled.getCode());
        orderBatchMapper.updateById(updateEntity);
        return true;
    }

    @Override
    public OrderBatch findByBatchId(String batchId) {
        return orderBatchMapper.selectOne(new LambdaQueryWrapper<OrderBatch>().eq(OrderBatch::getBatchId, batchId));
    }

    @Override
    public void updatePayStatusByBatchId(OrderBatch orderBatch,String batchId) {
        if(StringUtils.isEmpty(batchId)){
            BusinessErrorInfoEnum.ORDER_PAY_NOTIFY_NO_DATA.assertFail();
        }
        final OrderBatch updateEntity = new OrderBatch();
        updateEntity.setOrderStatus(OrderStatusEnum.cancelled.getCode());
        orderBatchMapper.update(orderBatch,new QueryWrapper<OrderBatch>().eq("batch_id",batchId));
    }
}
